package goodbaby.dklt.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.classic.core.activity.BaseActivity;
import com.classic.core.utils.DateUtil;
import com.classic.core.utils.StringUtil;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;
import com.tgd.gbb.uikit.ui.dialog.WLQQTimePicker;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

import goodbaby.dklt.R;
import goodbaby.dklt.adapter.AttendanceAdapter;
import goodbaby.dklt.bean.AttendanceBean;
import goodbaby.dklt.fragment.StudyFragment;

/**
 * Created by Jayden on 2016/4/24 16:44.
 *
 * @version V1.0
 * @Title: AttendanceActivity
 * @Description: 考勤页面/请假页面
 */
public class AttendanceActivity extends BaseActivity {

    private AttendanceAdapter adapter;
    private Button btnFind;
    private String endCurrentDate;
    private String endDate;
    private EditText etReason;
    private int fromItem;
    private ImageView ivBack;
    private String leaveReason;
    private LinearLayout llInput;
    private LinearLayout llListAttendance;
    private ListView lvAttendance;
    private RelativeLayout rlEndTime;
    private RelativeLayout rlStartTime;
    private String startCurrentDate;
    private String startDate;
    private PopupWindow timePopupWindow;
    private TextView tvEndTime;
    private TextView tvHistory;
    private TextView tvStartTime;
    private TextView tvTitle;
    private int type = 0;

    private void getAttendanceData() {
        if (StringUtil.isEmpty(startCurrentDate)) {
            ToastUtil.showToast(this, "开始时间不能为空");
            return;
        }
        if (StringUtil.isEmpty(endCurrentDate)) {
            ToastUtil.showToast(this, "结束时间不能为空");
            return;
        }
        long l1 = Long.parseLong(DateUtil.getTime(startCurrentDate));
        long l2 = Long.parseLong(DateUtil.getTime(endCurrentDate));
        if (l2 - l1 > 2592000000L) {
            ToastUtil.showToast(this, "选择时间不能超过30天");
            return;
        }
        if (l1 > l2) {
            ToastUtil.showToast(this, "开始时间不能大于结束时间");
            return;
        }
        ActionHelp.gbbQueryAttendance(this, DateUtil.getTime(startCurrentDate), DateUtil.getTime(endCurrentDate), new ObjectCallback<List<AttendanceBean>>() {
            @Override
            public void onError(int code) {
                ToastUtil.showToast(AttendanceActivity.this, "查询失败");
            }

            @Override
            public void onSuccess(List<AttendanceBean> response) {
                adapter = new AttendanceAdapter(AttendanceActivity.this, response);
                lvAttendance.setAdapter(adapter);
                llListAttendance.setVisibility(View.VISIBLE);
            }

            @Override
            public Type getType() {
                return new TypeToken<List<AttendanceBean>>() {
                }.getType();
            }
        });
    }

    private void submitLeaveMsg() {
        leaveReason = etReason.getText().toString().trim();
        if (StringUtil.isEmpty(startCurrentDate)) {
            ToastUtil.showToast(this, "开始时间不能为空");
            return;
        }
        if (StringUtil.isEmpty(endCurrentDate)) {
            ToastUtil.showToast(this, "结束时间不能为空");
            return;
        }
        if (StringUtil.isEmpty(leaveReason)) {
            ToastUtil.showToast(this, "请假理由不能为空");
            return;
        }
        long l1 = Long.parseLong(DateUtil.getTime(startCurrentDate));
        long l2 = Long.parseLong(DateUtil.getTime(endCurrentDate));
        if (l2 - l1 > 2592000000L) {
            ToastUtil.showToast(this, "选择时间不能超过30天");
            return;
        }
        if (l1 > l2) {
            ToastUtil.showToast(this, "开始时间不能大于结束时间");
            return;
        }
        ActionHelp.gbbSubmitLeave(this, DateUtil.getTime(this.startCurrentDate), DateUtil.getTime(endCurrentDate), leaveReason, new ObjectCallback<String>() {
                    @Override
                    public void onSuccess(String response) {
                        ToastUtil.showToast(AttendanceActivity.this, "提交成功");
                        finish();
                    }

                    @Override
                    public void onError(int code) {
                        if (code == 417) {
                            ToastUtil.showToast(AttendanceActivity.this, "请假失败，今天您的请假申请还没有通过审核哦");
                        } else {
                            ToastUtil.showToast(AttendanceActivity.this, "提交失败，请重试");
                        }
                    }

                    @Override
                    public Type getType() {
                        return new TypeToken<String>() {
                        }.getType();
                    }
                }
        );
    }

    public void TimeDialog(View paramView) {
        LayoutInflater mLayoutInflater = (LayoutInflater) this
                .getSystemService(LAYOUT_INFLATER_SERVICE);

        //right_pop為泡泡的布局
        View view = mLayoutInflater.inflate(R.layout.date_popupwindows, null);
        WLQQTimePicker localWLQQTimePicker = (WLQQTimePicker) view.findViewById(R.id.timepicker);
        localWLQQTimePicker.setDate(new Date().getTime());
        localWLQQTimePicker.init(new MyCallBack());
        timePopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        timePopupWindow.setFocusable(true);
        timePopupWindow.setBackgroundDrawable(new BitmapDrawable());
        timePopupWindow.setSoftInputMode(16);
        timePopupWindow.setOutsideTouchable(true);
        timePopupWindow.showAsDropDown(paramView);
    }

    public int getLayoutResId() {
        return R.layout.activity_attendance;
    }

    public void initData() {
        super.initData();
        this.fromItem = getIntent().getIntExtra("fromItem", 0);
    }

    public void initView() {
        super.initView();
        tvTitle = (TextView) findViewById(R.id.tv_common_title);
        ivBack = (ImageView) findViewById(R.id.iv_common_back);
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        tvHistory = (TextView) findViewById(R.id.tv_common_leave_history);
        tvHistory.setText("请假记录");
        tvHistory.setOnClickListener(this);
        llInput = (LinearLayout) findViewById(R.id.ll_input);
        btnFind = (Button) findViewById(R.id.btn_find);
        btnFind.setOnClickListener(this);
        llListAttendance = (LinearLayout) findViewById(R.id.ll_attendance_list);
        lvAttendance = (ListView) findViewById(R.id.lv_attendance);
        rlStartTime = (RelativeLayout) findViewById(R.id.rl_start_time);
        rlStartTime.setOnClickListener(this);
        rlEndTime = (RelativeLayout) findViewById(R.id.rl_end_time);
        rlEndTime.setOnClickListener(this);
        tvStartTime = (TextView) findViewById(R.id.tv_start_time);
        tvEndTime = (TextView) findViewById(R.id.tv_end_time);
        etReason = (EditText) findViewById(R.id.et_input_reason);
        if (fromItem == StudyFragment.fromAttendance) {
            tvTitle.setText("考勤");
            llInput.setVisibility(View.GONE);
            tvHistory.setVisibility(View.GONE);
            btnFind.setText("查询");
            llListAttendance.setVisibility(View.GONE);
        } else if (fromItem == StudyFragment.fromLeave) {
            tvTitle.setText("请假");
            this.llInput.setVisibility(View.VISIBLE);
            this.tvHistory.setVisibility(View.VISIBLE);
            this.btnFind.setText("请假");
            this.llListAttendance.setVisibility(View.GONE);
        }
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
    }

    protected void onDestroy() {
        super.onDestroy();
    }

    public void viewClick(View paramView) {
        super.viewClick(paramView);
        switch (paramView.getId()) {
            default:
                break;
            case R.id.iv_common_back:
                finish();
                break;
            case R.id.tv_common_leave_history:
                startActivity(new Intent(this, LeaveHistoryActivity.class));
                break;
            case R.id.btn_find:
                if (fromItem == 0) {
                    getAttendanceData();
                } else {
                    submitLeaveMsg();
                }
            case R.id.rl_start_time:
                type = 0;
                TimeDialog(rlStartTime);
                break;
            case R.id.rl_end_time:
                type = 1;
                TimeDialog(rlEndTime);
                break;
        }

    }

    public class MyCallBack
            implements WLQQTimePicker.CallBack {
        public MyCallBack() {
        }

        public void Cancel() {
            timePopupWindow.dismiss();
        }

        public void ConfirmAndGetData(String paramString) {
            if (type == 0) {
                tvStartTime.setText(paramString);
                startDate = paramString;
                startCurrentDate = paramString + ":00";
            } else {
                AttendanceActivity.this.tvEndTime.setText(paramString);
                endDate = paramString;
                endCurrentDate = paramString + ":00";
            }
            timePopupWindow.dismiss();

        }
    }
}
